var gulp = require('gulp');
var rename = require('gulp-rename');
var path = require('path');
var template = require('gulp-template');
var browserSync = require('browser-sync');
var reload = browserSync.reload;
/***Less refer**/
var less = require('gulp-less');
var LessAutoprefix = require('less-plugin-autoprefix');
var autoprefix = new LessAutoprefix({browsers: ['last 2 versions']});
var LessPluginCleanCSS = require('less-plugin-clean-css'),
    cleanCSSPlugin = new LessPluginCleanCSS({advanced: true});
var uglify = require('gulp-uglify');

/*** watch refer**/
var watch = require('gulp-watch');
var concat = require('gulp-concat');


/** image **/
var imagemin = require('gulp-imagemin');
var yargs = require('yargs').argv;
var postcss = require('gulp-postcss');
var autoprefixer = require('autoprefixer');

var server = require('gulp-express');

gulp.task('build:images', function () {
    gulp.src('src/images/*')
        // .pipe(imagemin())
        .pipe(gulp.dest('resource/images'))
});

gulp.task('build:css', function () {
    gulp.src('src/style/css/*.css')
        .pipe(gulp.dest('resource/style'))
})

gulp.task('build:less', ['build:css'], function () {
    return gulp.src('src/style/less/*.less')
        .pipe(less({
                plugins: [autoprefix],//cleanCSSPlugin
                paths: [path.join(__dirname, 'less', 'includes')]

            }).on('error', function (e) {
                console.error(e.message);
                this.emit('end');
            })
        )
        // .pipe(gulp.dest('resource/css'))
        .pipe(reload({stream: true}))
        .pipe(gulp.dest('resource/style/'))
});

gulp.task('build:font', function () {
    return gulp.src('src/fonts/*')
        .pipe(gulp.dest('resource/fonts'))
});


gulp.task('build:script', function () {
    gulp.src(['src/js/config.js', 'src/js/app-controller.js', 'src/js/constant.js'])
        .pipe(gulp.dest('resource/js'))
        .pipe(reload({stream: true}));

    gulp.src(['src/js/services.js', 'src/js/service/**/*.js'])
        .pipe(concat('services.js'))
        .pipe(gulp.dest('resource/js'))
        .pipe(reload({stream: true}));

    gulp.src(['src/js/filters.js', 'src/js/filter/**/*.js'])
        .pipe(concat('filters.js'))
        .pipe(gulp.dest('resource/js'))
        .pipe(reload({stream: true}));

    gulp.src(['src/js/directives.js', 'src/js/directive/**/*.js'])
        .pipe(concat('directives.js'))
        .pipe(gulp.dest('resource/js'))
        .pipe(reload({stream: true}));
})

gulp.task('build:lib', function () {
    gulp.src('src/deps-lib/**/*')
        .pipe(gulp.dest('resource/deps-lib'));
    return gulp.src('src/lib/**/*.js')
        .pipe(gulp.dest('resource/lib'))
});

gulp.task('build:index', function () {
    return gulp.src('src/index.html')
        // .pipe(gulp.dest('resource'))
        .pipe(rename('index.ejs'))
        .pipe(gulp.dest('views'))
        .pipe(reload({stream: true}));
});

gulp.task('build:page:html', function () {
    return gulp.src('src/page/**/*.html')
        .pipe(gulp.dest('resource/page'))
        // .pipe(rename('index.ejs'))
        // .pipe(gulp.dest('views'))
        .pipe(reload({stream: true}));
});

gulp.task('build:page:less', function () {
    return gulp.src('src/page/**/*.less')
        .pipe(less({
                plugins: [autoprefix],//cleanCSSPlugin
                paths: [path.join(__dirname, 'less', 'includes')]

            }).on('error', function (e) {
                console.error(e.message);
                this.emit('end');
            })
        )
        .pipe(reload({stream: true}))
        .pipe(gulp.dest('resource/page'))
});

gulp.task('build:page:js', function () {
    return gulp.src(['src/page/**/*.js','src/component/**/*.js'])
        .pipe(concat('controllers.js'))
        .pipe(gulp.dest('resource/js'))
        .pipe(reload({stream: true}));
});

gulp.task('build:component:html', function () {
    return gulp.src('src/component/**/*.html')
        .pipe(gulp.dest('resource/component'))
        // .pipe(rename('index.ejs'))
        // .pipe(gulp.dest('views'))
        .pipe(reload({stream: true}));
});
gulp.task('build:component:less', function () {
    return gulp.src('src/component/**/*.less')
        .pipe(less({
                plugins: [autoprefix],//cleanCSSPlugin
                paths: [path.join(__dirname, 'less', 'includes')]

            }).on('error', function (e) {
                console.error(e.message);
                this.emit('end');
            })
        )
        .pipe(reload({stream: true}))
        .pipe(gulp.dest('resource/component'))
});

gulp.task('build:page', ['build:page:html', 'build:page:js', 'build:page:less']);

gulp.task('build:component', ['build:component:html',  'build:component:less']);


gulp.task('build:project', ['build:less', 'build:font', 'build:script', 'build:lib', 'build:images'], function () {
    gulp.start('build:page');
    gulp.start('build:component');
});
gulp.task('build', ['build:project', 'build:index']);


gulp.task('watcher', ['build'], function () {
    gulp.watch('src/style/**/*', ['build:less']);
    gulp.watch('src/js/**/*.js', ['build:script']);
    gulp.watch('src/index.html', ['build:index']);
    gulp.watch('src/page/**/*.js', ['build:page:js']);
    gulp.watch('src/page/**/*.html', ['build:page:html']);
    gulp.watch('src/page/**/*.less', ['build:page:less', 'build:less']);
    gulp.watch('src/component/**/*.js', ['build:page:js']);
    gulp.watch('src/component/**/*.html', ['build:component:html']);
    gulp.watch('src/component/**/*.less', ['build:component:less','build:less']);

    // return gulp.watch(['**/*.html', 'css/**/*.css', 'js/**/*.js'], []);
});

// 监视文件改动并重新载入
gulp.task('serve', function () {
    // yargs.p = yargs.p || 6666;
    // browserSync.init({
    //     server: {
    //         baseDir: 'resource'
    //     },
    //     port: yargs.p
    // });
    gulp.start('watcher');
});

gulp.task('default', ['build'], function () {
    gulp.start('serve');
});

gulp.start('default');





